﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.MeiTuanPeiSong.BLL;
using SHKF.B2C.MeiTuanPeiSong.Model.CallBack;
using SHKF.B2C.MeiTuanPeiSong.Model.Dto;
using SHKF.B2C.MeiTuanPeiSong.Model.Dto.Shop;
using SHKF.B2C.MeiTuanPeiSong.Model.Dto.ShopArea;
using SHKF.B2C.MeiTuanPeiSong.Model.Entity.Shop;
using SHKF.B2C.MeiTuanPeiSong.Network;
using SHKF.B2C.Model.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 

namespace SHKF.B2C.MeiTuanPeiSong.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class ShopController : BaseController
    {
        private readonly HttpClientMeiTuanPeiSong httpClient;
        private readonly ShopBLL shopBLL;

        public ShopController(HttpClientMeiTuanPeiSong httpClient, 
            ShopBLL shopBLL,
            ILogger<ShopController> logger) : base(logger)
        {
            this.httpClient = httpClient;
            this.shopBLL = shopBLL;
        }
 
        public async Task<DataDto> Save(CBShopStatus shopStatus)
        {
            DataDto dto = new DataDto();
            ShopDtoV2 shopDto = await httpClient.ShopQuery(shopStatus.shop_id);

            int code = 0;
            string message = "";
            if (shopDto.code > 0)
            {
                code = shopDto.code;
                message = shopDto.message;
            }
            else
            {
                ShopMtpsEntity shop = shopDto.data;
                shop.shop_name = shopStatus.shop_name;
                shop.status = shopStatus.status;
                shop.pay_type_code = string.Join(',', shopDto.data.pay_type_codes); 
                base.WriteInformation("Save", JsonConvert.SerializeObject(shopDto.data) ); 
                long sn = shopBLL.Save(shop);
                if (sn <= 0)
                {
                    code = -1;
                    message = "数据没有保存成功";
                }
            }

            if( code > 0)
            {  
                dto.error = new Error() { code = code, msg = message };
            }

            return dto;
        }

        [HttpGet("query")]
        public ShopDto Query(string shopID)
        {
            ShopDto dto = new ShopDto(); 
            dto.data = this.shopBLL.GetData(shopID); 
            return dto;
        }

        /// <summary>
        /// 查询门店配送范围
        /// </summary>
        /// <param name="delivery_service_code"></param>
        /// <param name="shop_id"></param>
        /// <returns></returns>
        [HttpGet("area/query")]
        public async Task<ShopAreaDto> ShopAreaQuery(int delivery_service_code, string shop_id)
        {
            ShopAreaDto dto = new ShopAreaDto();
            MtpsShopAreaDto dto2 = await this.httpClient.ShopAreaQuery(delivery_service_code, shop_id);
 
            if (dto2.code == 0)
            {
                dto.data = dto2.data;
            }
            else
            {
                dto.error = new Error() { code = dto2.code, msg = dto2.message };
            }

            return dto;
        }


        /// <summary>
        /// 门店状态回调测试
        /// </summary>
        /// <param name="orderStatus"></param>
        /// <returns></returns>
        [HttpPost("status/callback")]
        public async Task<IActionResult> StatusCallback([FromForm] CBShopStatus shopStatus)
        {
            CodeDto okDto = new CodeDto();
            JsonResult jsonResult = new JsonResult(okDto);

            try
            { 
                if (shopStatus != null)
                {
                    await this.Save(shopStatus); 
                    base.WriteInformation("StatusCallback", JsonConvert.SerializeObject(shopStatus));
                }
                else
                {
                    base.WriteInformation("StatusCallback", "shopStatus是空的");
                }
            }
            catch (Exception ex)
            {
                okDto.code = -1;
                base.WriteException("Status", ex);
            }

            jsonResult.StatusCode = 200;
            return jsonResult;
        }
    }
}
